﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using SGP;

namespace Sprite_Editor
{
    public enum EmitterShape { Line, Circle, Square };

    class Emitter
    {
        public Emitter()
        {
            particles = new Particle[10000];
        }

        public void createparticles( int NumberOfParticles )
        {
            for (int i = 0; i < NumberOfParticles; i++)
            {
                particles[i] = new Particle(this);
                particles[i].Initialize();
            }
            maxparticles = NumberOfParticles;
            remainingParticles = NumberOfParticles;
        }

        public void Update(float deltaTime)
        {
            for (int i = 0; i < maxparticles; i++)
            {
                if (particles[i].IsActive)
                {
                    particles[i].Update(deltaTime);
                }
            }

            timesincespawn += deltaTime;
            if (timesincespawn >= spawnrate)
            {
                timesincespawn = 0.0f;
                if( looping == false && remainingParticles > 0)
                {
                    remainingParticles--;
                    for (int i = 0; i < maxparticles; i++)
                    {
                        if (particles[i].IsActive)
                            continue;
                        else
                        {
                            particles[i].IsActive = true;
                            break;
                        }
                    }
                }
                else if (looping)
                {
                    for (int i = 0; i < maxparticles; i++)
                    {
                        if (particles[i].IsActive)
                            continue;
                        else
                        {
                            particles[i].IsActive = true;
                            break;
                        }
                    }
                }
            }
        }

        public void Render()
        {
            for (int i = 0; i < maxparticles; i++)
            {
                if (particles[i].IsActive)
                {
                    particles[i].Render();
                }
            }
        }

        private int remainingParticles;

        public int RemainingParticles
        {
            get { return remainingParticles; }
            set { remainingParticles = value; }
        }

        private EmitterShape shape;

        public EmitterShape Shape
        {
            get { return shape; }
            set { shape = value; }
        }
        

        public Particle[] particles;

        private bool hasgravity;

        public bool HasGravity
        {
            get { return hasgravity; }
            set { hasgravity = value; }
        }

        private bool looping;

        public bool Looping
        {
            get { return looping; }
            set { looping = value; }
        }

        private int maxparticles;

        public int MaxParticles
        {
            get { return maxparticles; }
            set { maxparticles = value; }
        }

        private float posX;

        public float PosX
        {
            get { return posX; }
            set { posX = value; }
        }

        private float posY;

        public float PosY
        {
            get { return posY; }
            set { posY = value; }
        }

        private float initialspeedmin;

        public float InitialSpeedMin
        {
            get { return initialspeedmin; }
            set { initialspeedmin = value; }
        }

        private float initialspeedmax;

        public float InitialSpeedMax
        {
            get { return initialspeedmax; }
            set { initialspeedmax = value; }
        }

        private float lifetimemin;

        public float LifetimeMin
        {
            get { return lifetimemin; }
            set { lifetimemin = value; }
        }

        private float lifetimemax;

        public float LifetimeMax
        {
            get { return lifetimemax; }
            set { lifetimemax = value; }
        }

        private float offsetX;

        public float OffsetX
        {
            get { return offsetX; }
            set { offsetX = value; }
        }

        private float offsetY;

        public float OffsetY
        {
            get { return offsetY; }
            set { offsetY = value; }
        }

        private Color initialcolor;

        public Color InitialColor
        {
            get { return initialcolor; }
            set { initialcolor = value; }
        }

        private Color finalcolor;

        public Color FinalColor
        {
            get { return finalcolor; }
            set { finalcolor = value; }
        }

        private float initialscale;

        public float InitialScale
        {
            get { return initialscale; }
            set { initialscale = value; }
        }

        private float finalscale;

        public float FinalScale
        {
            get { return finalscale; }
            set { finalscale = value; }
        }

        private float initialrotation;

        public float InitialRotation
        {
            get { return initialrotation; }
            set { initialrotation = value; }
        }

        private float finalrotation;

        public float FinalRotation
        {
            get { return finalrotation; }
            set { finalrotation = value; }
        }

        private float anglerange;

        public float AngleRange
        {
            get { return anglerange; }
            set { anglerange = value; }
        }

        private float emitterrotation;

        public float EmitterRotation
        {
            get { return emitterrotation; }
            set { emitterrotation = value; }
        }

        private float spawnrate;

        public float SpawnRate
        {
            get { return spawnrate; }
            set { spawnrate = value; }
        }
        
        private float timesincespawn;
    }
}
